# -*- coding: utf-8 -*-
import unittest
from structures.hashtable import HashTable


class HashTableTest(unittest.TestCase):

    def test_add_and_retrieve_item(self):
        hash = HashTable()
        key = "one"
        hash.set(key, 1)

        self.assertEqual(1, hash.get(key))

    def test_add_and_retrieve_two_items(self):
        pairs = [["one", 1], ["two", 2]]
        hash = HashTable()
        for pair in pairs:
            key = pair[0]
            data = pair[1]
            hash.set(key, data)

        count = 1
        for pair in pairs:
            key = pair[0]
            self.assertEqual(count, hash.get(key))
            count += 1

    def test_add_and_retrieve_fifteen_items(self):
        pairs = [["one", 1], ["two", 2], ["three", 3],
                ["four", 4], ["five", 5], ["six", 6],
                ["seven", 7], ["eight", 8], ["nine", 9],
                ["ten", 10], ["eleven", 11], ["twelve", 12],
                ["thirteen", 13], ["fourteen", 14], ["fifteen", 15]]

        hash = HashTable()
        for pair in pairs:
            key = pair[0]
            data = pair[1]
            hash.set(key, data)

        count = 1
        for pair in pairs:
            key = pair[0]
            self.assertEqual(count, hash.get(key))
            count += 1

    def test_add_and_retrieve_fifteen_items_similar_keys(self):
        hash = HashTable()
        n = 100
        for i in range(n):
            key = 'V' + str(i)
            hash.set(key, i)

        for i in range(n):
            key = 'V' + str(i)
            self.assertEqual(i, hash.get(key))

    def test_add__fifteen_items_similar_keys_retrieve_different(self):
        hash = HashTable()
        n = 100
        for i in range(n):
            key = 'V' + str(i)
            hash.set(key, i)

        self.assertEqual(None, hash['does not exist'])
        self.assertEqual(None, hash['V101'])
        self.assertEqual(None, hash['V1.1'])

    def test_add_some_items_and_get_values_list(self):
        hash = HashTable()
        n = 15
        for i in range(n):
            key = 'V' + str(i)
            hash.set(key, i)

        values = hash.get_values()

        self.assertEqual(n, len(values))

    def test_add_items_to_force_rehashing_and_get_values_list(self):
        hash = HashTable()
        n = 1000
        for i in range(n):
            key = 'V' + str(i)
            hash.set(key, i)

        values = hash.get_values()

        self.assertEqual(n, len(values))
